$(document).ready(function() {
	mui.init();
	mui.plusReady(function() {
		plus.nativeUI.showWaiting("数据加载中...");
		var wx = plus.webview.currentWebview();
		var signx = false;
		var hl = $(document).innerHeight();
		var dayin_sign = false;
		var remark_s = "";
		$("html, body").height(hl + "px");
		var token = localStorage.getItem("token");
		require(weburl + model_fundAuthLists, "json", "post", {
			token: token,
			type: "901",
			auth_no: wx.authNo,
			out_order_no: wx.orderNum
		}, function(response) {
			console.log(JSON.stringify(response));
			var data = response.data[0];
			if(response.status == "success") {
				$(".pay_licensing").val(data.amount + "元");
				$(".order_id").val(data.out_order_no);
				$(".serial_id").val(data.auth_no);
				$(".pay_account").val(data.payer_logon_id);
				$(".pay_time").val(data.created_at);
				$(".pay_status").val(pay_status(data.pay_status));
				$(".bzxx").val(data.fu_remark);
				$(".use_price").val(data.pay_amount);
				remark_s = data.fu_remark;
				if(data.pay_status == 1) {
					$(".but").removeClass("active_dis");
				}
//				Bluetooth();
				plus.nativeUI.closeWaiting();
				//点击打印按钮
				mui("body").on("tap", ".refund", function() {
					plus.nativeUI.showWaiting("正在退款，请勿退出...");
					require(weburl + model_shouquandayin, "json", "post", {
						token: localStorage.getItem("token"),
						out_order_no: data.out_order_no,
						auth_no: wx.authNo
					}, function(response) {
						plus.nativeUI.closeWaiting();
						if(response.status == "success") {
							var str = response.data[0].print_body;
							var reg = /\\n/g;
							str = str.replace(reg, "\n");
							mui.closePopups();
							mui.confirm("是否打印小票？", " ", ["打印", "关闭"], function(e) {
								if(e.index == 0) {
									if(dayin_sign == false) {
										console.log(response);
										printTicket(response.data[0].print_array);
//										ticket(response.data[0].store_name, str, response.data[0].out_order_no + "-");
										dayin_sign = true;
										setTimeout(function() {
											dayin_sign = false;
										}, 3000);
									}
									return false;
								} else {

								}
							}, "div");
						} else {
							mui.alert(response.msg, " ", "关闭", function() {}, "div");
						}
					}, function() {
						plus.nativeUI.closeWaiting();
						mui.alert("请求错误，请稍后再试", " ", "关闭", function() {}, "div");
					})
				})

				//点击结算按钮
				mui("body").on("tap", ".checkout", function() {
					$(".remarks_info, .pay_settle").blur();
					if(data.pay_status == 1 && $(".remarks_info").val() && $(".pay_settle").val()) {
						//弹出框内容
						var str = "<p><label>预授权金额：</label><span class='depPrice'>" + data.amount + "</span>元</p><p><label>消费金额：</label><span class='depPrice'>" + Number($(".pay_settle").val()).toFixed(2) + "</span>元</p><p><label>消费时间：</label>" + data.created_at + "</p><p><label>备注信息：</label>" + $(".remarks_info").val() + "</p><p style='font-size:16px;text-align:center;color:red;'>请确认消费金额与实际金额一致！</p><p style='text-align:center;margin-top:15px;font-size:16px;color:#F0AD4E;margin-bottom:0;padding-bottom:10px;'>一旦操作，剩余资金将解冻退回</br>无法被追回</p>";
//						str+="<p style='font-size:16px;text-align:center;color:red;'>请确认消费金额与实际金额一致！</p>"; 
//						str+="<p style='text-align:center;margin-top:15px;font-size:16px;color:#F0AD4E;margin-bottom:0;padding-bottom:10px;'>一旦操作，剩余资金将解冻退回</br>无法被追回</p>"; 
						mui.confirm(str, "确认结算？", ["确认结算", "取消"], function(e) {
							if(e.index == 0) {
								plus.nativeUI.showWaiting("正在结算，请勿退出...");
								require(weburl + model_FundAuthTrade, "json", "post", {
									token: token,
									remarks: $(".remarks_info").val(),
									pay_num: $(".pay_settle").val(),
									auth_no: data.auth_no
								}, function(re) {
									plus.nativeUI.closeWaiting();
									if(re.status == "success") {
										var str = re.data[0].print_body;
										var reg = /\\n/g;
										str = str.replace(reg, '\n');
										printTicket(re.data[0].print_array);
//										ticket(re.data[0].store_name, str, re.data[0].out_trade_no);
										mui.closePopups();
										mui.confirm("结算成功，是否打印小票？", " ", ["打印", "关闭"], function(e) {
											if(e.index == 0) {
												if(signx == false) {
													printTicket(re.data[0].print_array);
//													ticket(re.data[0].store_name, str, re.data[0].out_trade_no);
													signx = true;
													setTimeout(function() {
														signx = false;
													}, 3000);
												}
												return false;
											} else {
												window.location.reload();
											}

										}, "div");
									} else {
										mui.alert(re.msg, " ", "关闭", function() {
											window.location.reload();
										}, "div");
									}
								}, function() {
									plus.nativeUI.closeWaiting();
									mui.alert("请求错误，请稍后再试", " ", "关闭", function() {}, "div");
								})
							}
						}, "div");
					} else if(!$(".pay_settle").val()) {
						mui.alert("<p style='text-align:center;font-size:16px;color:#000;'>消费金额不能为空！</p>", " ", "确认", function() {}, "div");
					} else if(!$(".remarks_info").val()) {
						mui.alert("<p style='text-align:center;font-size:16px;color:#000;'>备注信息不能为空！</p>", " ", "确认", function() {}, "div");
					} else {
						mui.alert("该订单已关闭", " ", "确定", function() {}, "div");
					}
				})

				//点击解冻退回
				mui("body").on("tap", ".thaw", function() {
					$(".remarks_info, .pay_settle").blur();
					var signx = false;
					//输入框内容
					var conf = "<p class='returnTitle' style='text-align:center;'><span class='mui-icon mui-icon-info'></span>警告！</p>";
					conf+="<p style='font-size:16px;text-align:center;'>退回金额：<span style='font-size:18px;color:red;'>"+data.amount+"<span>元</p>";
					conf+="<p style='font-size:16px;text-align:center;color:red;'>请确认客户没有消费！</p>";
					conf+="<p style='text-align:center;margin-top:15px;font-size:16px;color:#F0AD4E;'>一旦退回，资金将无法被追回</br>请谨慎操作</p>";
					//预授权模板
					var str = "<p><label>预授权金额：</label><span class='depPrice'>" + data.amount + "</span>元</p><p><label>消费时间：</label>" + data.created_at + "</p><p><label>备注信息：</label>" + $(".remarks_info").val() + "</p><p><label>支付密码：</label><input class='pay_settle pay_pas' type='password' placeholder='请输入支付密码' style='font-size:12px;'></p>";
					if($(".remarks_info").val()) { //弹出第一个框
						mui.confirm(conf, " ", ["确认", "取消"], function(ev) {
							$(".mui-popup-inner").css("padding","15px");
							if(ev.index == 0) { //如果内容不为空且点击确认按钮
								mui.confirm(str, "确认解冻退回？", ["取消", "全额退回"], function(e) {
									$(".pay_pas").blur();
									if(e.index == 1) {  //点击全额退回
										var remark_i = $(".remark_i").val();
										var pay_pas = $(".pay_pas").val();

										if($(".pay_pas").val()) {
											plus.nativeUI.showWaiting("正在解冻退回，请勿退出...");
											require(weburl + model_Unfreeze, "json", "post", {
												token: token,
												paystr: $(".pay_pas").val(),
												auth_no: data.auth_no,
												remarks: $(".remarks_info").val()
											}, function(res) {
												plus.nativeUI.closeWaiting();
												if(res.status == "success") {
													console.log(JSON.stringify(response));
													var str = res.data[0].print_body;
													var reg = /\\n/g;
													str = str.replace(reg, '\n');
													printTicket(res.data[0].print_array);
//													ticket(res.data[0].store_name, str, res.data[0].out_trade_no + "-");
													mui.closePopups();
													mui.confirm("解冻退回成功，是否打印小票？", " ", ["打印", "关闭"], function(e) {
														if(e.index == 0) {
															if(signx == false) {
																printTicket(res.data[0].print_array);
//																ticket(res.data[0].store_name, str, res.data[0].out_trade_no + "-");
																signx = true;
																setTimeout(function() {
																	signx = false;
																}, 3000);
															}
														} else {
															window.location.reload();
														}

													}, "div");
												} else {
													mui.closePopups();
													mui.alert(res.msg, " ", "关闭", function() {}, "div");
												}
											}, function() {
												plus.nativeUI.closeWaiting();
												mui.closePopups();
												mui.alert("请求错误，请稍后再试", " ", "关闭", function() {}, "div");
											})
										} else if(!$(".pay_pas").val()) {
											mui.alert("<p style='text-align:center;font-size:16px;color:#000;'>支付密码不能为空！</p>", " ", ["确认"], function() {}, "div");
										}
									}
								}, "div");
							}
						}, "div");
						$(".mui-popup-inner").css("padding","0");
						$(".mui-popup-inner").css("padding-bottom","15px");
					} else {
						mui.alert("<p style='text-align:center;font-size:16px;color:#000;'>备注信息不能为空！</p>", " ", ["确认"], function() {}, "div");
					}

				})
			} else {
				plus.nativeUI.closeWaiting();
				mui.alert(response.msg, " ", "关闭", function() {}, "div");
			}

		}, function() {
			plus.nativeUI.closeWaiting();
			mui.alert("系统繁忙，请稍后重试");
		})

		/*
		 * 点击修改备注
		 */
		mui("body").on("tap", ".changeBz", function() {
			//弹出输入框
			if($(".pay_status").val() == "未结算") {
				mui.confirm("<input type='text' value='" + remark_s + "' class='change_remark' >", "修改备注", ["确认", "取消"], function(e) {
					$(".change_remark").blur();
					if(e.index == 0 && $(".change_remark").eq(0).val()) { //点击了确认按钮并且内容不为空
						require(weburl + model_remark, "json", "post", {
							token: localStorage.getItem("token"),
							auth_no: wx.authNo,
							out_order_no: wx.orderNum,
							remarks: $(".change_remark").eq(0).val()
						}, function(response) {
							//成功函数
							mui.alert(response.msg, " ", "关闭", function() {
								if(response.status == "success") { //如果修改成功则刷新页面
									window.location.reload();
								}
							}, "div")
						}, function() {
							//失败函数
							mui.alert("修改备注信息失败", " ", "关闭", function() {}, "div");
						})
					} else if(e.index == 0 && (!$(".change_remark").eq(0).val())) {
						mui.alert("备注内容不能为空", " ", "关闭", function() {}, "div");
					}
				}, "div")
				return false;
			}
			mui.alert("该订单已完成,不可修改", " ", "关闭", function() {}, "div");

		})

		//支付状态函数
		function pay_status(num) {
			var sta = "";
			switch(num) {
				case 1:
					sta = "未结算";
					break;
				case 2:
					sta = "已取消";
					break;
				case 3:
					sta = "等待支付";
					break;
				case 4:
					sta = "已结算";
					break;
				case 5:
					sta = "已退回";
					break;
			}
			return sta;
		}
	})

})